To extract a file from a zip to memory variable (for instance, a Byte array), use the following function. First make sure to add these Imports (Visual Basic)/using (C#) statements at the top of the code:
| Visual Basic |
Copy Code
|
|---|---|
Imports C1.C1Zip and Imports System.IO |
|
| C# |
Copy Code
|
|---|---|
using C1.C1Zip; and using System.IO |
|
Then add the following code:
| Visual Basic |
Copy Code
|
|---|---|
Private Function GetDataFromZipFile(zipFileName As String, entryName As String) As Byte()
' Get the entry from the zip file.
Dim zip As New C1ZipFile()
zip.Open(zipFileName)
Dim ze As C1ZipEntry = zip.Entries(entryName)
' Copy the entry data into a memory stream.
Dim ms As New MemoryStream()
Dim buf(1000) As Byte
Dim s As Stream = ze.OpenReader()
Try
While True
Dim read As Integer = s.Read(buf, 0, buf.Length)
If read = 0 Then
Exit While
End If
ms.Write(buf, 0, read)
End While
Finally
s.Dispose()
End Try
s.Close()
' Return result.
Return ms.ToArray()
End Function
|
|
| C# |
Copy Code
|
|---|---|
private byte[] GetDataFromZipFile(string zipFileName, string entryName) { // Get the entry from the zip file. C1ZipFile zip = new C1ZipFile(); zip.Open(zipFileName); C1ZipEntry ze = zip.Entries[entryName]; // Copy the entry data into a memory stream. MemoryStream ms = new MemoryStream(); byte[] buf = new byte[1000]; using (Stream s = ze.OpenReader()) { for (;;) { int read = s.Read(buf, 0, buf.Length); if (read == 0) break; ms.Write(buf, 0, read); } } // There's no need to call close because of the C# 'using' // statement above but in VB this would be necessary. //s.Close(); // Return result. return ms.ToArray(); } |
|